package com.way.capture.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.animation.AccelerateInterpolator;

import androidx.core.content.ContextCompat;

import com.way.capture.R;
import com.way.capture.base.BaseActivity;

public class SplashActivity extends BaseActivity {
    private static final String TAG = "SplashActivity";
    private static final ArgbEvaluator ARGB_EVALUATOR = new ArgbEvaluator();
    private AnimatorSet mAnimatorSet;

    private void next() {
        Log.d(TAG, "onAnimationEnd...");
        mAnimatorSet.removeAllListeners();
        startActivity(new Intent(this, MainActivity.class));
        finish();
        overridePendingTransition(R.anim.scroll_in, R.anim.scroll_out);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: " + hashCode());
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) > 0) {
            finish();
            return;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initViews();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_splash;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void initViews() {
        ColorDrawable backgroundColorDrawable = new ColorDrawable(Color.BLACK);
        View foreMaskView = findViewById(R.id.fore_mask);
        foreMaskView.setBackground(backgroundColorDrawable);
        final int toColor = ContextCompat.getColor(this, R.color.colorAccentExtra);
        final Window window = getWindow();
        //状态栏动画
        ObjectAnimator statusBarColorAnimator = ObjectAnimator.ofInt(window,
                "statusBarColor", window.getStatusBarColor(), toColor);
        statusBarColorAnimator.setEvaluator(ARGB_EVALUATOR);

        //虚拟按键动画
        ObjectAnimator navigationBarColorAnimator = ObjectAnimator.ofInt(window,
                "navigationBarColor", window.getNavigationBarColor(), toColor);
        navigationBarColorAnimator.setEvaluator(ARGB_EVALUATOR);

        //背景动画
        ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(backgroundColorDrawable,
                "color", ARGB_EVALUATOR, toColor);

        if (mAnimatorSet == null) {
            mAnimatorSet = new AnimatorSet();
        }
        mAnimatorSet.setStartDelay(200L);
        mAnimatorSet.setDuration(500L);
        mAnimatorSet.setInterpolator(new AccelerateInterpolator());
        mAnimatorSet.playTogether(statusBarColorAnimator, navigationBarColorAnimator, backgroundColorAnimator);

        mAnimatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                next();
            }
        });
        mAnimatorSet.start();
    }

}
